package com.xy.kafka.controller;

import com.xy.kafka.service.SparkStreamingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据可视化控制器，提供实时数据的REST API
 */
@RestController
@RequestMapping("/api/visualization")
public class DataVisualizationController {

    @Autowired
    private SparkStreamingService sparkStreamingService;

    /**
     * 获取实时统计数据
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Long>> getStats() {
        return ResponseEntity.ok(sparkStreamingService.getStats());
    }

    /**
     * 获取单词计数数据
     */
    @GetMapping("/wordCounts")
    public ResponseEntity<List<Map<String, Object>>> getWordCounts() {
        return ResponseEntity.ok(sparkStreamingService.getWordCounts());
    }

    /**
     * 获取最近消息
     */
    @GetMapping("/recentMessages")
    public ResponseEntity<List<Map<String, Object>>> getRecentMessages() {
        return ResponseEntity.ok(sparkStreamingService.getRecentMessages());
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Boolean>> health() {
        return ResponseEntity.ok(Map.of("healthy", sparkStreamingService.isHealthy()));
    }
}